<?php

declare (strict_types=1);

namespace app\beautiful\controller\cashier;
use app\BaseController;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\StaffModel;
use app\common\logic\TokenLogic;

class Main extends BaseController {
    public function login()
    {
        $mobile=$this->request->param('mobile');
        $password=$this->request->param('password');
        if(empty($mobile)){
            return $this->error('请输入手机号');
        }
        if(empty($password)){
            return $this->error('请输入密码');
        }
        $where=[
            ['mobile','=',$mobile],
            ['password','=',md5($password)]
        ];
        if(null === $staff = StaffModel::where($where)->find()){
            return $this->error('手机号或者密码不正确');
        }
        if($staff->is_delete !=0) return $this->error('手机号或者密码不正确');
        if($staff->status != 1) return $this->error('您已经离职了');
        if($staff->is_cashier_login ==0) return $this->error('您没有权限登录');

        $auth_token=TokenLogic::getStaffToken($staff->staff_id);
        return $this->success(['auth_token'=>$auth_token]);
    }

    public function home()
    {
        $shop= $this->request->shop;
        $staff=$this->request->staff;
        $datas=[
            'shop_name'=>$shop->name,
            'staff_name'=>$staff->name,
        ];
        return $this->success(['datas'=>$datas]);
    }

    public function getMemberId()
    {
        $mobile=$this->request->param('mobile');
        if(empty($mobile) || !is_mobile($mobile)) return $this->error('请输入手机号');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['mobile','=',$mobile]
        ];
        if(null === $member = MemberModel::where($where)->find()) return $this->error('未找到此会员');
        return $this->success(['member_id'=>$member->member_id]);
    }
}
